package com.library.controller.admin;

import com.library.pojo.Book;
import com.library.pojo.PageResult;
import com.library.pojo.Result;
import com.library.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminBook")
@RequestMapping("/admin/book")
public class BookController {
    @Autowired
    private BookService bookService;
    /**
     * 分页查询书籍
     */
    @GetMapping("/page")
    public Result pageQuery(@RequestParam(defaultValue = "1")Integer page
            ,@RequestParam(defaultValue = "10")Integer pageSize,String bookName,
                            String author,String publisher,String category){
        PageResult result =
                bookService.pageQuery(page,pageSize,bookName,author,publisher,category);
        return Result.success(result);
    }
    /**
     * 添加图书
     */
    @PostMapping
    public Result save(@RequestBody Book book){
        bookService.add(book);
        return Result.success();
    }
    /**
     * 删除图书
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Long> ids) {
        bookService.delete(ids);
        return Result.success();
    }
    /**
     * 根据id查询图书
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        Book book = bookService.getById(id);
        return Result.success(book);
    }
    /**
     * 修改书籍
     */
    @PutMapping
    public Result update(@RequestBody Book book){
        bookService.update(book);
        return Result.success();
    }
}
